package domain;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.springframework.format.annotation.DateTimeFormat;

@Entity
@Access(AccessType.PROPERTY)
@Table(indexes = {@Index(columnList = "startMoment"), @Index(columnList = "finishMoment")})
public class Auction extends DomainEntity {
	
	//Constructors

	public Auction() {
		super();
		bids = new HashSet<Bid>();
			
	}

	//Attributes
	private Date startMoment;
	private Date finishMoment;
	private Money startingPrice;

	@NotNull
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(pattern = "dd/MM/yyyy HH:mm")
	public Date getFinishMoment() {
		return finishMoment;
	}
	public void setFinishMoment(Date finishMoment) {
		this.finishMoment = finishMoment;
	}
	@NotNull
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(pattern = "dd/MM/yyyy HH:mm")
	public Date getStartMoment() {
		return startMoment;
	}

	public void setStartMoment(Date startMoment) {
		this.startMoment = startMoment;
	}
	@Valid
	@NotNull
	public Money getStartingPrice() {
		return startingPrice;
	}

	public void setStartingPrice(Money startingPrice) {
		this.startingPrice = startingPrice;
	}
	

	// Relationships//
	private Customer customer;
	private Painting painting;
	private Collection<Bid> bids;
	private Bid winningBid;
	
	@NotNull
	@Valid
	@ManyToOne(optional=false)
	public Painting getPainting() {
		return painting;
	}
	public void setPainting(Painting painting) {
		this.painting = painting;
	}
	@OneToMany(mappedBy = "auction", cascade = CascadeType.ALL)
	public Collection<Bid> getBids() {
		return bids;
	}
	public void setBids(Collection<Bid> bids) {
		this.bids = bids;
	}
	@NotNull
	@Valid
	@ManyToOne(optional = false)
	public Customer getCustomer() {
		return customer;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	
	@Valid
	@OneToOne
	public Bid getWinningBid() {
		return winningBid;
	}
	
	public void setWinningBid(Bid winningBid) {
		this.winningBid = winningBid;
	}

}
